package br.odb.bzk3.android;

import java.util.ArrayList;
import java.util.Vector;

import br.odb.libscene.Actor;
import br.odb.libscene.ActorConstants;
import br.odb.libscene.SceneObject;
import br.odb.utils.math.Vec3;
import bzk3.geometry.ObjMesh;

/**
 * 
 * @author daniel
 */
public class GameActor extends Actor {
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	public static PositionalMediaPlayer walkSound = PositionalMediaPlayer
			.getFor(new Vec3(), R.raw.steps);
	/**
	 * 
	 */
	private ObjMesh mesh;
	/**
	 * 
	 */
	public boolean playable;
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	public GameActor() {
		super();
		mesh = new ObjMesh();
		playable = false;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param actor
	 */
	public GameActor(GameActor actor) {
		super(actor);

		mesh = new ObjMesh(actor.getMeshes());
		playable = actor.playable;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public void consume(int cmdCode) {

		super.consume(cmdCode);
		if (playable)
			walkSound.playUnique();
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public void fire() {
		GameActor ga = new Projectile(this);
		setGenerated(ga);
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public Actor generateCopy() {
		return new GameActor(this);
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @return
	 */
	public int getDirection() {

		int dir = Math.round( ( getAngleXZ() ) / 90.0f);

		while (dir < 0)
			dir += 4;

		while (dir >= 4)
			dir -= 4;

		return dir;

	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @return
	 */
	public int getDiscreetAngle() {
		
		int toReturn = (int) (getAngleXZ() / 22.5f);
		
		while ( toReturn < 0 )
			toReturn += 16;
		
		return toReturn;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @return
	 */
	public ArrayList<ObjMesh> getMesh() {
		ArrayList<ObjMesh> vector = new ArrayList<ObjMesh>();
		vector.add(mesh);
		return vector;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param object
	 */
	public void hit(SceneObject object) {
		undo();
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param obj
	 */
	public void setMesh(ObjMesh obj) {
		mesh = obj;
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public void tick() {
		super.tick();
		//if (!playable)
		//	consume(ActorConstants.MOVE_N);
	}
	// ------------------------------------------------------------------------------------------------------------
	/**
	 * 
	 */
	@Override
	public void undo() {

		super.undo();
		if (!playable) {
			consume(ActorConstants.TURN_L);
			consume(ActorConstants.TURN_L);
		}
	}
	// ------------------------------------------------------------------------------------------------------------
	public float getDY() {
		return 5.5f;
	}
}
